package com.boco.blch.analysiser.compliance;

import org.apache.log4j.Logger;

import com.boco.blch.analysiser.exception.SerialNoNotFoundException;
import com.boco.blch.analysiser.util.ConfigUtil;

public class ComplianceFactory {
	private static Logger logger = Logger.getLogger(ComplianceFactory.class);
	private static String[] PACKAGE = { "", "manager", "data" };

	public static ComplianceResolved getInstance(String seriaNo, String plantform, String nodeType)
			throws SerialNoNotFoundException {
		StringBuilder className = new StringBuilder();
		className.append(ConfigUtil.getBasePackageName()).append(".").append(plantform).append(".")
				.append(PACKAGE[Integer.valueOf(nodeType)]).append(".").append(seriaNo);

		ComplianceResolved type = null;
		try {
			Class<?> subtype = Class.forName(className.toString());
			type = (ComplianceResolved) subtype.newInstance();
		} catch (Exception e) {
			logger.error(e);
		}
		return type;
	}

	public static ComplianceResolved getInstanceBySerialNo(String serialNo) throws SerialNoNotFoundException {
		ComplianceResolved type = null;
		if (!ConfigUtil.getProperties().containsKey(serialNo)) {
			throw new SerialNoNotFoundException("还未添加此编号对应的合规检查项，请确认");
		}
		String clazz = ConfigUtil.getProperties().getProperty(serialNo);
		try {
			Class<?> subtype = Class.forName(clazz);
			type = (ComplianceResolved) subtype.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return type;
	}

}
